package com.shadow.baiqun.delegate.room.adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shadow.baiqun.R;
import com.shadow.baiqun.delegate.room.check.BoardInfoEntity;

import java.util.List;

/**
 * @author zy
 * on 2018/6/19
 */
public class CheckTaskCommitAdapter extends BaseQuickAdapter<BoardInfoEntity, BaseViewHolder> {
    private OnCommitListener mListener;

    public CheckTaskCommitAdapter(int layoutResId, @Nullable List<BoardInfoEntity> data, OnCommitListener listener) {
        super(layoutResId, data);
        mListener = listener;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final BoardInfoEntity item) {
        helper.setText(R.id.et_part_name, item.getPartName());
        helper.setText(R.id.et_part_code, item.getPartNum());
        helper.setText(R.id.et_supplier_name, String.valueOf(item.getPartCount()));
        helper.setText(R.id.et_good_parts_count, String.valueOf(item.getPartCount()));
        ((EditText)helper.getView(R.id.et_good_parts_count))
                .addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
        helper.setText(R.id.et_bad_part_count, String.valueOf(item.getBadCount()));
        helper.getView(R.id.tv_commit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String partCount = ((EditText) helper.getView(R.id.et_good_parts_count)).getText().toString();
                String goodCount = ((EditText) helper.getView(R.id.et_good_parts_count)).getText().toString();
                String badCount = ((EditText) helper.getView(R.id.et_bad_part_count)).getText().toString();
                if (mListener != null && !goodCount.isEmpty() && !badCount.isEmpty()) {
                    mListener.onCommit(item, Integer.parseInt(partCount),
                            Integer.parseInt(goodCount), Integer.parseInt(badCount));
                }
            }
        });
    }

    public interface OnCommitListener {
        void onCommit(BoardInfoEntity entity,int partCount, int goodCount, int badCount);
    }
}
